package com.example.socialapp.ui.main

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.socialapp.Screen
import com.example.socialapp.ui.main.components.BottomNavBar
import com.example.socialapp.ui.main.screens.ChatScreen
import com.example.socialapp.ui.main.screens.ContactsScreen
import com.example.socialapp.ui.main.screens.DiscoverScreen
import com.example.socialapp.ui.main.screens.HomeScreen
import com.example.socialapp.ui.main.screens.ProfileScreen

/**
 * 主界面，包含底部导航栏和各个Tab页面
 *
 * @param onLogout 退出登录回调
 * @param onNavigateToSettings 导航到设置界面回调
 * @param onNavigateToCreatePost 导航到创建动态界面回调
 * @param onNavigateToSearch 导航到搜索界面回调
 * @param onNavigateToPostDetail 导航到帖子详情界面回调
 * @param onNavigateToUserProfile 导航到用户个人资料界面回调
 * @param onNavigateToChat 导航到聊天界面回调
 * @param onNavigateToNotification 导航到通知界面回调
 * @param onNavigateToBookmark 导航到收藏界面回调
 * @param onNavigateToRecommendation 导航到内容推荐界面回调
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainScreen(
    onLogout: () -> Unit,
    onNavigateToSettings: () -> Unit,
    onNavigateToCreatePost: () -> Unit = {},
    onNavigateToSearch: () -> Unit = {},
    onNavigateToPostDetail: (String) -> Unit = {},
    onNavigateToUserProfile: (String) -> Unit = {},
    onNavigateToChat: (String, String) -> Unit = { _, _ -> },
    onNavigateToNotification: () -> Unit = {},
    onNavigateToBookmark: () -> Unit = {},
    onNavigateToRecommendation: () -> Unit = {}
) {
    val navController = rememberNavController()
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = navBackStackEntry?.destination?.route
    
    Scaffold(
        bottomBar = {
            BottomNavBar(
                currentRoute = currentRoute,
                onNavItemClick = { route ->
                    navController.navigate(route) {
                        // 避免创建多个实例
                        popUpTo(navController.graph.findStartDestination().id) {
                            saveState = true
                        }
                        // 避免相同目的地多次入栈
                        launchSingleTop = true
                        // 恢复状态
                        restoreState = true
                    }
                }
            )
        }
    ) { innerPadding ->
        MainNavHost(
            navController = navController,
            onLogout = onLogout,
            onNavigateToSettings = onNavigateToSettings,
            onNavigateToCreatePost = onNavigateToCreatePost,
            onNavigateToSearch = onNavigateToSearch,
            onNavigateToPostDetail = onNavigateToPostDetail,
            onNavigateToUserProfile = onNavigateToUserProfile,
            onNavigateToChat = onNavigateToChat,
            onNavigateToNotification = onNavigateToNotification,
            onNavigateToBookmark = onNavigateToBookmark,
            onNavigateToRecommendation = onNavigateToRecommendation,
            modifier = Modifier.padding(innerPadding)
        )
    }
}

/**
 * 主导航宿主，包含各个Tab页面
 */
@Composable
private fun MainNavHost(
    navController: NavHostController,
    onLogout: () -> Unit,
    onNavigateToSettings: () -> Unit,
    onNavigateToCreatePost: () -> Unit,
    onNavigateToSearch: () -> Unit,
    onNavigateToPostDetail: (String) -> Unit,
    onNavigateToUserProfile: (String) -> Unit,
    onNavigateToChat: (String, String) -> Unit,
    onNavigateToNotification: () -> Unit,
    onNavigateToBookmark: () -> Unit,
    onNavigateToRecommendation: () -> Unit,
    modifier: Modifier = Modifier
) {
    NavHost(
        navController = navController,
        startDestination = Screen.Main.route,
        modifier = modifier
    ) {
        composable(Screen.Main.route) {
            HomeScreen(
                onNavigateToSettings = onNavigateToSettings,
                onNavigateToCreatePost = onNavigateToCreatePost,
                onNavigateToSearch = onNavigateToSearch,
                onNavigateToPostDetail = onNavigateToPostDetail,
                onNavigateToNotification = onNavigateToNotification,
                onNavigateToBookmark = onNavigateToBookmark,
                onNavigateToRecommendation = onNavigateToRecommendation
            )
        }
        
        composable(Screen.Chat.route) {
            ChatScreen()
        }
        
        composable(Screen.Contacts.route) {
            ContactsScreen()
        }
        
        composable(Screen.Discover.route) {
            DiscoverScreen()
        }
        
        composable(Screen.Profile.route) {
            ProfileScreen(
                onLogout = onLogout,
                onNavigateToSettings = onNavigateToSettings,
                onNavigateToBookmark = onNavigateToBookmark
            )
        }
    }
} 